﻿var eventId;

$(function () {
    ajaxCall('../DataService.asmx/GetCurrentEvent', false, "",
            function (msg) {

                eventId = msg.d.EventID;
            },
            function () {
                alert("Error happend while reading data from database");
            }
        );
    getAllTracks();
});

function getAllTracks() {
    $('#Tracks').empty();
    ajaxCall('../DataService.asmx/GetAllTracks', false, '{"eventId" : "' + eventId + '"}',
                function (msg) {

                    if (msg.d == null) {
                        $('#tracks').html("The agenda for this code camp event has not been created yet!!");
                    }
                    else {
                        for (i in msg.d) {
                            var tracks = [
                                { trackID: msg.d[i].TrackID, trackName: msg.d[i].TrackName, trackColor: msg.d[i].TrackColor, description: msg.d[i].Description }
                            ];

                            $('#TrackTemplateNames').tmpl(tracks).appendTo('#TrackNames');
                        }
                        getTimeSlots(msg.d[0].TrackID, msg.d[0].TrackColor);
                    }
                },
                function () {
                    alert("Error in reading from database");
                }
            );
}

function getTimeSlots(id, color) {
    var sessions = getSessionsInTrack(id);
    ajaxCall('../DataService.asmx/GetTimeSlotsForEvent', false, '{"eventId": "' + eventId + '"}',
                function (msg) {
                    $('#unavailableTimeSlots').empty();
                    if (msg.d == null) {
                        $('#unavailableTimeSlots').html("TimeSlots not found!");
                    }
                    else {
                        $('#TrackTimeSlots').append(" <div class='timeSlotsLeft' style='height:20px; padding-top:0; padding-bottom:0; padding-left:5px; padding-right:5px; text-align:center; font-weight:bold;'>TIME</div>");
                        $('#TrackSchedule').append("<div class='breaks' style='background-color:" + color + "; height:20px; padding:0; opacity:1; font-weight:bold;'>Track schedule</div>");
                   
                        for (i in msg.d) {
                            if (msg.d[i].Type == 'Presentation') {
                                for (var j = 0; j < sessions.length; j++) {
                                    if (sessions[j].TimeSlot.From == msg.d[i].From) {
                                        var ses = [
                                            { ID: sessions[j].ID, Title: sessions[j].Title, Speakers: sessions[j].Speakers, Color: color }
                                        ];
                                        $('#SessionTemplate').tmpl(ses).appendTo('#TrackSchedule');
                                    }
                                }
                            } else {
                                var other = [
                                            { Type: msg.d[i].Type, Link: msg.d[i].Link }
                                        ];
                                $('#OtherTemplate').tmpl(other).appendTo('#TrackSchedule');
                            }
                            var timeSlots = [
                                { timeSlotID: msg.d[i].ID, From: JSONTime(msg.d[i].From), To: JSONTime(msg.d[i].To), Type: msg.d[i].Type }
                            ];
                            $('#TimeSlotsTemplate').tmpl(timeSlots).appendTo('#TrackTimeSlots');
                        }
                     }
                },
                function () {
                    alert("Error happend while reading data from database");
                }
            );

}

function getSessionsInTrack(id) {
    var sessionsList = [];
    ajaxCall('../DataService.asmx/GetSessionsInTrack', false, '{"trackId":"' + id + '", "eventId":"' + eventId + '"}',
                function (msg) {
                    for (i in msg.d) {
                        var speakers = msg.d[i].Speakers[0].FirstName + " " + msg.d[i].Speakers[0].LastName;
                        for (var j = 1; j < msg.d[i].Speakers.length; j++) {
                            speakers = speakers + " & " + msg.d[i].Speakers[j].FirstName + " " + msg.d[i].Speakers[j].LastName;
                        }
                        var sessions = { ID: msg.d[i].ID, Title: msg.d[i].Title, Speakers: speakers, TimeSlot: msg.d[i].TimeSlot };
                        sessionsList[i] = sessions;
                    }

                },
                function () {
                    alert("Error happend while reading data");
                }
            );

    return sessionsList;
}

function getTimeSlotsForTrack(id, color) {
    $('#TrackSchedule').empty();
    $('#TrackTimeSlots').empty();
    getTimeSlots(id, color);
}